﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using System.Web;
using YouHP.Model.Group;

namespace web.Pages.xml
{
    public partial class symptonlist_xml : System.Web.UI.Page
    {
        
        protected List<UserInfo> UserDataListLatest;

        protected List<SymptomInfo> SymptomDataList;
        protected List<DiseaseInfo> DiseaseLst;

        
        protected List<SymptomHistoryInfo> CurrentSymptomList;

        protected PatientRecordInfo PrData;
        #region 用户基本信息
        protected UserInfo UserData = new UserInfo();// 当前用户的信息
        protected UserGroupInfo UserGroupInfo = new UserGroupInfo(); //当前用户组信息   
        #endregion


        protected int SymptomCount = 0;
        protected int SymptomPageCount = 0;
        protected int SymptomPageSize = 15;
        protected int SymptomPageIndex = 1;
        protected string SymptomPagingString;

        protected SymptomSearchConditon cond = null;
        protected void ResponseContext(int iCode , string sMsg)
        {
            //生产xml文档
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = ("    ");
            Response.ContentType = "text/xml";
            using (XmlWriter wr = XmlWriter.Create(Response.OutputStream, settings))
            {
                // Write XML data.
                wr.WriteStartElement("result");
                wr.WriteAttributeString("code", iCode.ToString());
                wr.WriteAttributeString("msg", sMsg);
                if ( iCode == 0 )
                {
                    wr.WriteStartElement("symptoms");
                    wr.WriteAttributeString("count", SymptomCount.ToString());
                    foreach (SymptomInfo info in SymptomDataList)
                    {
                        wr.WriteStartElement("symptom");
                        wr.WriteAttributeString("id", info.Id.ToString());
                        wr.WriteAttributeString("name", info.Name.ToString());
                        info.Intro = info.Intro == "" ? "暂无简介" : info.Intro;
                        wr.WriteAttributeString("desc", info.Intro);
                        wr.WriteAttributeString("count", info.PatientCount.ToString());
                        wr.WriteEndElement();
                    }
                }
                


                wr.WriteEndElement();
                wr.WriteEndElement();
                wr.Flush();
            }
        }
        protected bool CheckLogin()
        {
            UserData.Id = CookiesTools.GetUserIdCookie();
            if (UserData.Id > 0) //表示永久登陆的用户
            {
                UserData.UserName = CookiesTools.GetUserNameCookie();   //直接从cookie中获取用户名
                if (UserData.UserName.Length <= 0)
                {
                    UserData.Id = -1;    //如果不存在用户名，则为无效的cookie
                    return false;
                }
            }
            return true;
        }
        protected bool CheckParam(ref string sMsg)
        {
            cond = new SymptomSearchConditon();

            cond.DiseaseId = RequestTools.GetFormInt("dszid", 0);
            cond.DiseaseName = RequestTools.GetFormString("dszname").Trim();
            cond.SymptomName = RequestTools.GetFormString("symptomname").Trim();

            SymptomPageIndex = RequestTools.GetFormInt("pno", 1);
            SymptomPageSize = RequestTools.GetFormInt("pnum", 15);
            return true;
        }
        protected void Handle()
        {
           
            if (!CheckLogin())
            {
                ResponseContext(-1000, "你还没有登录");
                return;
            }
            string sErrMsg = "";
            if (!CheckParam(ref sErrMsg))
            {
                ResponseContext(-1001, sErrMsg);
                return;
            }


            PrData = PatientBLL.Instance.GetPatientRecordInfo(UserData.Id);

            if (PrData.Id <= 0)
            {
                ResponseContext(-100, "请先填写患病基本信息");                
                return;
            }

            CurrentSymptomList = PatientSymptomBLL.Instance.GetCurrentSymptomList(PrData.Id);
            StringBuilder bd = new StringBuilder();
            foreach (SymptomHistoryInfo sym in CurrentSymptomList)
            {
                bd.Append(sym.SymptomId);
                bd.Append(',');
            }
            if ( bd.Length > 0 )
            {
                cond.NoAllowed = bd.ToString(0, bd.Length - 1);
            }
            string sOrder = "id";
            if (cond.DiseaseId == 0 )
            {
                sOrder = string.Format("abs([Symptom].[ClassDiseaseId]-{0})", PrData.DiseaseId);
            }
            SymptomDataList = SymptomBLL.Instance.GetSymptomList(SymptomPageSize, SymptomPageIndex, cond, sOrder, out SymptomCount);

            ResponseContext(0, "成功");

        }

       
        protected void Page_Load(object sender, EventArgs e)
        {
            Handle();
        }
    }
}
